package me.dr0n3.skyAdopt.Utils;

import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.event.block.SignChangeEvent;

/**
 * @author dr0n3
 * @date 17.04.2012
 */
public class SignHelper 
{
    public static boolean isValidSkyAdoptSign(Block block)
    {
        if(!(block.getState() instanceof Sign))
        {
            return false;
        }
        else
        {
            Sign sign = (Sign) block.getState();
            if(sign.getLine(0).equalsIgnoreCase(ChatColor.BLUE + "[skyAdopt]"))
            {
                if(sign.getLine(1).equalsIgnoreCase("wolf") || sign.getLine(1).equalsIgnoreCase("cat"))
                {
                    if(sign.getLine(2).equalsIgnoreCase("free"))
                    { 
                        return true;
                    }
                    else
                    {
                        try
                        {
                            Double.parseDouble(sign.getLine(2));
                            return true;
                        }
                        catch(IndexOutOfBoundsException | NumberFormatException e)
                        {
                            return false;
                        }
                    }
                }
                else
                    return false;
            } 
            return false;
        }
    }
    
    public static boolean isValidSkyAdoptSign(SignChangeEvent e)
    {
            if(e.getLine(0).equalsIgnoreCase("[skyAdopt]"))
            {
                if(e.getLine(1).equalsIgnoreCase("wolf") || e.getLine(1).equalsIgnoreCase("cat"))
                {
                    if(e.getLine(2).equalsIgnoreCase("") || e.getLine(2).equalsIgnoreCase("free"))
                    { 
                        return true;
                    }
                    else
                    {
                        try
                        {
                            Double.parseDouble(e.getLine(2));
                            return true;
                        }
                        catch(IndexOutOfBoundsException | NumberFormatException ex)
                        {
                            return false;
                        }
                    }
                }
                else
                    return false;
            }
            return false;
    }
    
    public static boolean isWolf(Sign sign)
    {
        if(sign.getLine(1).equalsIgnoreCase("wolf"))
        {
            return true;
        }
        
        return false;
    }
    
    public static boolean isCat(Sign sign)
    {
        if(sign.getLine(1).equalsIgnoreCase("cat"))
        {
            return true;
        }
        
        return false;
    }
    
    public static double getPrice(Sign sign)
    {
        if(isFree(sign))
            return 0;
        else
            return Double.parseDouble(sign.getLine(2));
    }
    
    public static boolean isFree(Sign sign)
    {
        if(sign.getLine(2).isEmpty() || sign.getLine(2).equalsIgnoreCase("free"))
            return true;
        else
            return false;
    }
    
    public static boolean isFree(SignChangeEvent e)
    {
        if(e.getLine(2).isEmpty() || e.getLine(2).equalsIgnoreCase("free"))
            return true;
        else
            return false;
    }
            
}
